package it.uniroma2.art.owlart.langsearch.analysis;

import it.uniroma2.art.owlart.langsearch.index.RDFIndexManager;

import java.io.Reader;

import org.apache.lucene.analysis.CharTokenizer;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.Version;

/**
 * A tokenizer based on the label separator defined in {@link RDFIndexManager#_labelSplittingSeq}<br/>
 * Note that at the moment this value is hard coded to §! see issues in the source code of this class
 * 
 * @author Armando Stellato &lt;stellato@info.uniroma2.it&gt;
 *
 */
public class SingleCharLabelSeparatorTokenizer extends CharTokenizer {
	
	private int separatingCharType;  
	
	 /**
	   * Construct a new SingleCharLabelSeparatorTokenizer. * @param matchVersion Lucene version
	   * to match See {@link <a href="#version">above</a>}
	   * 
	   * @param in
	   *          the input to split up into tokens
	   */
	  public SingleCharLabelSeparatorTokenizer(Version matchVersion, Reader in) {
	    super(matchVersion, in);
	    initializeSeparator();	
	  }

	  /**
	   * Construct a new SingleCharLabelSeparatorTokenizer using a given {@link AttributeSource}.
	   * 
	   * @param matchVersion
	   *          Lucene version to match See {@link <a href="#version">above</a>}
	   * @param source
	   *          the attribute source to use for this {@link Tokenizer}
	   * @param in
	   *          the input to split up into tokens
	   */
	  public SingleCharLabelSeparatorTokenizer(Version matchVersion, AttributeSource source, Reader in) {
	    super(matchVersion, source, in);
	    initializeSeparator();
	  }

	  /**
	   * Construct a new SingleCharLabelSeparatorTokenizer using a given
	   * {@link org.apache.lucene.util.AttributeSource.AttributeFactory}.
	   *
	   * @param
	   *          matchVersion Lucene version to match See
	   *          {@link <a href="#version">above</a>}
	   * @param factory
	   *          the attribute factory to use for this {@link Tokenizer}
	   * @param in
	   *          the input to split up into tokens
	   */
	  public SingleCharLabelSeparatorTokenizer(Version matchVersion, AttributeFactory factory, Reader in) {
	    super(matchVersion, factory, in);
	    initializeSeparator();
	  }
	  
	  private void initializeSeparator() {
		  char[] chars = RDFIndexManager._labelSplittingSeq.toCharArray();
		  if (chars.length>1)
			  throw new IllegalStateException("trying to use a: " + SingleCharLabelSeparatorTokenizer.class + " when RDFIndexManager._labelSplittingSeq is not a single char");
		  separatingCharType = Character.getType(chars[0]);
		 //  System.out.println("char code for label separator: " + separatingCharType);
	  }
	  
	  
	  
	  /** Collects only characters which do not satisfy
	   * {@link Character#isWhitespace(int)}.*/
	  @Override
	  protected boolean isTokenChar(int c) {
		// System.out.println("char code: " + c);
	    return (c != 167); 
	    // check hot it is that the separator for § is 167 and not 28
	    		//separatingCharType);
	  }
	
}
